package com.bbx.taxi.client.Adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Menu.SetActivity;
import com.bbx.taxi.client.DB.Util.DBcolumns;
import com.bbx.taxi.client.Util.FormatUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 支付的费用明细列表
 *
 */
public class PayDetailsAdapter extends BaseAdapter {
	public static final String PAYDETAIL_NAME = "paydetail_name";
	public static final String PAYDETAIL_MARK = "paydetail_mark";//标记说明，默认不显示（左边）
	public static final String PAYDETAIL_MARK2 = "paydetail_mark2";//标记说明，默认不显示（右边）
	public static final String PAYDETAIL_DISCOUNT = "paydetail_discount";//折扣
	public static final String PAYDETAIL_MONEY = "paydetail_money";
	public static final String PAYDETAIL_TYPE = "paydetail_type";
	public static final String TYPE_PRICE="1";
	public static final String TYPE_REDUCE="0";
	public static final String TYPE_SCHEDULE="2";
	public static final String TYPE_THANK="3";
	public static final String TYPE_DISCOUNT="4";
	private List<Map<String, String>> list;
	private Context main;
	/**
	 * 支付的费用明细列表
	 *
	 */
	public PayDetailsAdapter(Context context,List<Map<String, String>> list) {
		this.list=list;
	   main=context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
		
	}
	/**
	 * 获取总优惠
	 */
	public double getTotalReduce(){
		double reduce = 0;
		for(int i=0;i<list.size();i++){
			String type=list.get(i).get(PAYDETAIL_TYPE);
			double money=Double.parseDouble(list.get(i).get(PAYDETAIL_MONEY));
			if(type.equals(TYPE_REDUCE)){
				reduce=reduce+money;
			}
		}
		return reduce;
	}
	/**
	 * 获取总价格
	 */
	public double getTotalPrice(){
		double price = 0;
		for(int i=0;i<list.size();i++){
			String type=list.get(i).get(PAYDETAIL_TYPE);
			double money=Double.parseDouble(list.get(i).get(PAYDETAIL_MONEY));
			if(type.equals(TYPE_PRICE)){
				price=price+money;
			}
		}
		return price;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if(convertView==null){      
		LayoutInflater inflater=LayoutInflater.from(main);
		convertView=(View)inflater.inflate(R.layout.item_paydetail,null);        
		viewholder=new ViewHolder();
		viewholder.tv_name=(TextView)convertView.findViewById(R.id.tv_name);	
		viewholder.tv_mark=(TextView)convertView.findViewById(R.id.tv_mark);	
		viewholder.tv_mark2=(TextView)convertView.findViewById(R.id.tv_mark2);	
		viewholder.tv_money=(TextView)convertView.findViewById(R.id.tv_money);	
		viewholder.iv_base_discount=(TextView)convertView.findViewById(R.id.iv_base_discount);	
		viewholder.layout_money=(RelativeLayout)convertView.findViewById(R.id.layout_money);	
		convertView.setTag(viewholder);	
		}
		else viewholder=(ViewHolder)convertView.getTag();
		viewholder.tv_name.setText(list.get(position).get(PAYDETAIL_NAME));
		String type=list.get(position).get(PAYDETAIL_TYPE);
		String money=list.get(position).get(PAYDETAIL_MONEY);
		if(type.equals(TYPE_REDUCE)){
			viewholder.tv_money.setTextColor(main.getResources().getColor(R.color.red));
			viewholder.tv_money.setText(String.format(main.getResources().getString(R.string.price),"-"+money));
		}else if(type.equals(TYPE_PRICE)){
			viewholder.tv_money.setTextColor(main.getResources().getColor(R.color.black));
			viewholder.tv_money.setText(String.format(main.getResources().getString(R.string.price),money));
		}else if(type.equals(TYPE_SCHEDULE)){
			viewholder.tv_money.setTextColor(main.getResources().getColor(R.color.red));
			viewholder.tv_money.setText(String.format(main.getResources().getString(R.string.price),money));
		}else if(type.equals(TYPE_THANK)){
			viewholder.tv_money.setTextColor(main.getResources().getColor(R.color.red));
			viewholder.tv_money.setText(String.format(main.getResources().getString(R.string.price),"+"+money));
		}else if(type.equals(TYPE_DISCOUNT)){
			viewholder.tv_money.setTextColor(main.getResources().getColor(R.color.red));
			viewholder.tv_money.setText(money);
		}
		//mark1
		String mark=list.get(position).get(PAYDETAIL_MARK);
		if(mark!=null&&!mark.equals("")){
			viewholder.tv_mark.setVisibility(View.VISIBLE);
			viewholder.tv_mark.setText(mark);
		}else{
			viewholder.tv_mark.setVisibility(View.GONE);
		}
		//mark2
		
		//折扣
		String base_discount=list.get(position).get(PAYDETAIL_DISCOUNT);
		if(base_discount!=null&&!base_discount.equals("")){
			viewholder.iv_base_discount.setVisibility(View.VISIBLE);
			viewholder.iv_base_discount.setText(base_discount);
		}else{
			viewholder.iv_base_discount.setVisibility(View.GONE);
		}
		
		
		double double_money=0;
		try{double_money=Double.parseDouble(money);}catch(Exception e){
			double_money=-1;
		}
		//价钱
		if(money==null||money.equals("")||double_money==0){
			viewholder.layout_money.setVisibility(View.GONE);
		}else{
			viewholder.layout_money.setVisibility(View.VISIBLE);
		}

		return convertView;
		
}
	public class ViewHolder{                   
		TextView tv_name,tv_money,tv_mark,tv_mark2;//tv_mark是在左边，tv_mark2是在右边
		TextView iv_base_discount;
		RelativeLayout layout_money;
	}
}
